<?php
include 'header.php';

// 主页旁边的显示
if($ban_type != ''){
  $ban_type_cond = "WHERE btype_id = {$ban_type}";
}

$top_ban = $db->selectOne('bantype', '*', $ban_type_cond);


if (!empty($ban_type)) {
  $ban_cond = "INNER JOIN nnd_bantype ON nnd_banner.ban_type = nnd_bantype.btype_id WHERE nnd_banner.ban_type = {$ban_type} LIMIT {$offset},{$limit}";
  $count_cond = "WHERE ban_type = {$ban_type}";
  $count = $db->selectOne('banner', 'COUNT(ban_id)', $count_cond)[0]; //数据总条数
  $banner = $db->selectAll('banner', '*', $ban_cond); //显示的记录数
} else {
  $ban_cond = "LIMIT {$offset},{$limit}";
  $count = $db->selectOne('banner', 'COUNT(ban_id)')[0]; //数据总条数
  $banner = $db->selectAll('banner', '*', $ban_cond); //显示的记录数
}

$page_info = [
  'limit' => $limit,
  'size' => $size,
  'current' => $current,
  'count' => $count,
  'class' => 'pagination'
];
$pageobj = Page::getInstance($page_info);
$page = $pageobj->showpage();

// 单个删除
if (!empty($_GET['del_ban_id'])) {
  $id = $_GET['del_ban_id'];
  $del_cond = "WHERE ban_id = '{$id}'";
  $res = $db->delData('banner', $del_cond);
  if ($res) {
    echo "<script>alert('删除成功!');window.location.href='banner_list.php';</script>";
  } else {
    echo "<script>alert('删除失败!');</script>";
  }
}

// 批量删除
if(!empty($_POST['del_all'])){
  $del_ids = $_POST['idarr'];
  for($i=0; $i<count($del_ids); $i++){
    $dels_cond = "WHERE ban_id = '{$del_ids[$i]}'";
    $res = $db->delData('banner', $dels_cond);
    if($res){
      echo "<script>alert('删除成功！');window.location.href='banner_list.php'</script>";
    }else{
      echo "<script>alert('删除失败！');window.location.href='banner_list.php'</script>";
    }
  }
}


?>

<!-- Start: Content -->
<section id="content">
  <div id="topbar" class="affix">
    <ol class="breadcrumb">
      <li><a href="#"><span class="glyphicon glyphicon-home"></span></a></li>
      <li class="<?php if ($ban_type == '') {
                    echo 'active';
                  } ?>">轮播图管理</li>
      <?php if ($ban_type != '') {
        echo "<li class='active'>{$top_ban['btype_name']}</li>";
      } ?>
    </ol>
  </div>
  <div class="container">
    <div class="row">
      <div class="col-md-12">
        <div class="panel">
          <div class="panel-heading">
            <div class="panel-title"><?php if ($ban_type != '') {
                                        echo $top_ban['btype_name'];
                                      } else {
                                        echo '轮播图列表';
                                      } ?></div>
            <a href="banner_add.php" class="btn btn-info btn-gradient pull-right"><span class="glyphicons glyphicon-plus"></span> 添加轮播图</a>
          </div>
          <form action="banner_list.php" method="post">
            <div class="panel-body">
              <?php  ?>
              <h2 class="panel-body-title">互联网</h2>
              <table class="table table-striped table-bordered table-hover dataTable">
                <tr class="active">
                  <th class="text-center" width="100"><input type="checkbox" value="" id="checkall" class=""> 全选</th>
                  <th>轮播图名称</th>
                  <th>轮播图</th>
                  <th>添加时间</th>
                  <th width="200">操作</th>
                </tr>
                <?php foreach ($banner as $val) { ?>
                  <tr class="success">
                    <td class="text-center"><input type="checkbox" value="<?php echo $val['ban_id']; ?>" name="idarr[]" class="cbox"></td>
                    <td><?php echo $val['ban_name']; ?></td>
                    <td><img src="<?php echo ROOT . '/Admin/' . $val['ban_url']; ?>" width="200px" alt=""></td>
                    <td><?php echo date('Y-m-d', $val['ban_time']); ?></td>
                    <td>
                      <div class="btn-group">
                        <a href="banner_edit.php?ban_id=<?php echo $val['ban_id']; ?>" class="btn btn-default btn-gradient"><span class="glyphicons glyphicon-pencil"></span></a>
                        <a onclick="return confirm('确定要删除吗？');" href="?del_ban_id=<?php echo $val['ban_id']; ?>" class="btn btn-default btn-gradient dropdown-toggle"><span class="glyphicons glyphicon-trash"></span></a>
                      </div>
                    </td>
                  </tr>
                <?php } ?>
              </table>

              <div class="pull-left">
                <button type="submit" name="del_all" value="del_all" class="btn btn-default btn-gradient pull-right delall"><span class="glyphicons glyphicon-trash"></span></button>
              </div>

              <?php echo $page; ?>

            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</section>
<!-- End: Content -->
</div>
<!-- End: Main -->
</body>

</html>